using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace deWSP.UI.WebControls.WebParts
{
    [Guid("bd8246e4-cc7a-4a39-ac32-40da8d9e0661")]
    public class deControlContainerWebPart : deFramework.Base.BaseWebPart 
    {
     
        #region "Events - OnInit, OnLoad" 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            try
            {
                if (this.UserControlPath.Trim().Length > 0)
                {
                    try
                    {
                        this._embeddedControl = this.EmbedAndIntegrateUserControl(this.UserControlPath);
                    }
                    catch (Exception exception)
                    {
                        this.ErrorMessage += string.Format("{0} failed to load: [[Message]]: {1}\n\r [[Stack Trace]]: {2}", 
                            this.UserControlPath,
                            exception.ToString(), exception.StackTrace);
                    }
                }
            }
            catch (Exception exception)
            {
                this.ErrorMessage += string.Format("{0} failed to add as embedded control : {1}\n\r",
                    this.UserControlPath, exception.ToString());
            }
        }





        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (this.ErrorMessage.Length == 0)
            {
                try
                {
                    if (this.ControlIntegrated)
                        this.PassthroughPartPropertiesToEmbeddedUserControl();
                }
                catch (Exception exception)
                {
                    this.ErrorMessage += string.Format("; {0} failed to add to controls array: {1}\n\r",
                        this.UserControlPath, exception.ToString());
                }
                base.OnLoad(e);
            }
        }

        #endregion 

        #region "Properties - UserControlPath, UserControlParameters"

        private string _UserControlPath = String.Empty; //"~/_controltemplates/Welcome.ascx";
        private string _UserControlParameters = String.Empty;


        /// <summary>
        /// 
        /// </summary>
        [Personalizable(PersonalizationScope.User),
         WebBrowsable(true),
         WebDisplayName("User Control Path"),
         WebDescription("Set User Control Path for web part. "),
         System.ComponentModel.Category("User Control Wrapper")]
        public string UserControlPath
        {
            get { return this._UserControlPath; }
            set
            {
                try
                {
                    if (this.ControlIntegrated)
                        throw new Exception("You may not reassign the UserControlPath of a web part once its been integrated."); 
                    else 
                        this._UserControlPath = value;
                }
                catch (Exception ex)
                {
                    string errorText = ex.Message;
                }

            }
        }

        /// <summary>
        /// 
        /// </summary>
        [Personalizable(PersonalizationScope.User),
         WebBrowsable(true),
         WebDisplayName("User Control Parameters"),
         WebDescription("Set User Control Parameters for on-the-fly custom settings"),
         System.ComponentModel.Category("User Control Wrapper")]
        public string UserControlParameters
        {
            get { return this._UserControlParameters; }
            set
            {
                try
                {
                    this._UserControlParameters = value;
                    //gridview_Refresh(ddViewDate, System.EventArgs.Empty);
                }
                catch (Exception ex)
                {
                    string errorText = ex.Message;
                }

            }
        }







        #endregion

        #region "public - EmbeddedControl, ControlIntegrated"





        /// <summary>
        /// 
        /// </summary>
        public deFramework.Base.BaseUserControl EmbeddedControl
        {
            get
            {
                return _embeddedControl;
            }
        }
        private deFramework.Base.BaseUserControl _embeddedControl = null;

        /// <summary>
        /// 
        /// </summary>
        public bool ControlIntegrated
        {
            get
            {
                return _embeddedControlIntegrated;
            }
        }
        private bool _embeddedControlIntegrated = false; 


        #endregion

        #region "private - EmbedAndIntegrateUserControl, PassthroughPartPropertiesToEmbeddedUserControl"


        private deFramework.Base.BaseUserControl EmbedAndIntegrateUserControl(string userControlPath)
        {

            System.Web.UI.Control ctl = this.Page.LoadControl(this._UserControlPath);
            this.Controls.Add(ctl);


            deFramework.Base.BaseUserControl _intControl =
                (deFramework.Base.BaseUserControl)ctl;

            _intControl.WebPartIndicator = true;
            _intControl.MyContext = this.MyContext;
            this._embeddedControlIntegrated = true; 

            return _intControl;
        }


        private void PassthroughPartPropertiesToEmbeddedUserControl()
        {
            if (this.EmbeddedControl.UserControlParameters.Length > 0 && this.UserControlParameters.Length > 0)
                this.EmbeddedControl.UserControlParameters = this.UserControlParameters;

        }

        #endregion 


    }
}



#region "Dead Code" 


#endregion 